package org.changwang.List;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;

/**
 * @ClassName ListIteratorUse
 * @Description 迭代器iterator使用
 * @Author c.w
 * @Date 2025/6/10 18:18
 * @Version 1.0.0
 */
public class ListIteratorUse {
	public static void main(String[] args) {
		//listIteratorUse();
		//nullListIterator();
		iteratorInIterator();
	}

	/**
	 * 迭代器在迭代器中的使用
	 */
	private static void iteratorInIterator(){
		List<User> userList = initList();
		Iterator<User> iterator = userList.iterator();
		while (iterator.hasNext()) {
			User user = iterator.next();
			System.out.println("iterator:"+user.getName());
			Iterator<User> iterator2 = userList.iterator();
			while (iterator2.hasNext()) {
				User user1 = iterator2.next();
				if (user1.getName().equals("5")){
					iterator.remove();
				}
				System.out.println("in iterator:"+user1.getName());
			}
		}
	}


	private static void listIteratorUse() {
		System.out.println("listIteratorUse");
		List<User> list = new ArrayList<User>();
		list.add(new User("1", "1",29));
		list.add(new User("2", "2", 29));
		list.add(new User("3", "3", 29));
		list.add(new User("4", "4",  29));
		list.add(new User("5", "5",  29));
		list.add(new User("6", "5",  29));
		list.add(new User("7", "5",  29));
		list.add(new User("8", "5",  29));
		list.add(new User("9", "5",  29));

		Iterator<User> iterator = list.iterator();
		while (iterator.hasNext()) {
			User user = iterator.next();
			System.out.println(user);
		}
	}

	private static void nullListIterator() {
		System.out.println("nullListIterator");
		List<User> list = null;

		Iterator<User> iterator = list.iterator();
		while (iterator.hasNext()) {
			User user = iterator.next();
			System.out.println(user);
		}
	}

	private static List<User> initList(){
		System.out.println("初始化list");
		List<User> list = new ArrayList<User>();
		list.add(new User("1", "1",29));
		list.add(new User("2", "2", 29));
		list.add(new User("3", "3", 29));
		list.add(new User("4", "4",  29));
		list.add(new User("5", "5",  29));
		list.add(new User("6", "5",  29));
		list.add(new User("7", "5",  29));
		list.add(new User("8", "5",  29));
		list.add(new User("9", "5",  29));
		return list;
	}
}